package com.test.languagetest;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import com.pojo.Product;

public class HibernateTestCriteria {

	public static void main(String[] args) {

		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();

		String name = "phone";
		/*
		 * 使用Criteria进行数据查询。 与HQL和SQL的区别是Criteria 完全是 面向对象的方式在进行数据查询，将不再看到有sql语句的痕迹
		 * 
		 */
		Criteria criteria = session.createCriteria(Product.class);
		criteria.add(Restrictions.like("name", "%" + name + "%"));

		List<Product> ps = criteria.list();
		for(Product p : ps) {
			System.out.println(p.getName());
		}
		
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}
